package org.jsbd.boss.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.MiguVideoConfigBean;
import org.jsbd.boss.domian.MiguVideoDaylimitBean;
import org.jsbd.boss.domian.MiguVideoDaylimitVO;
import org.jsbd.boss.service.IMiguVideoDaylimitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/miguVideoDaylimit")
public class MiguVideoDaylimitController {

  private static final Logger logger = LoggerFactory.getLogger(MiguVideoDaylimitController.class);

  @Resource
  private IMiguVideoDaylimitService miguVideoDaylimitService;

  /**
   * 日限配置列表
   * @return
   */
  @RequestMapping(value = "/")
  public String daylimit() {
    return "miguVideoDaylimit/list";
  }

  /**
   * 
   * 日限配置数据
   * @param model
   * @return
   */
  @RequestMapping(value = "/listJson", method = RequestMethod.POST)
  @ResponseBody
  public Map<String, Object> listJson(DataGridModel page, MiguVideoDaylimitVO mmdVO) {
	  String sort = mmdVO.getSort();
	  String order = mmdVO.getOrder();
	  if (StringUtils.isBlank(sort) && StringUtils.isBlank(order)) {
		  mmdVO.setSort("executeTime");
		  mmdVO.setOrder("DESC");
	  }
	  return miguVideoDaylimitService.findDataWithPage(page, mmdVO);
  }
  
  /**
   * 跳转新增页面
   * @return
   */
  @RequestMapping(value = "/add", method = RequestMethod.GET)
  public String toAddPage() {
    return "miguVideoDaylimit/add";
  }
  
  /**
   * 新增
   * @return
   */
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  @ResponseBody
  public void add(HttpServletResponse response, MiguVideoDaylimitBean mmdBean) {
	try {
		String name = mmdBean.getName();
		MiguVideoConfigBean miguVideoConfigBean = miguVideoDaylimitService.hasName(name);
		if (miguVideoConfigBean == null) {
			ResponseUtils.responseMarkNotExists(response);
		} else {
			mmdBean.setIsExecute(0);
			mmdBean.setMark(miguVideoConfigBean.getChannelId() + "_" + miguVideoConfigBean.getVersionCode());
			miguVideoDaylimitService.save(mmdBean);
			ResponseUtils.responseSuccess(response);
		}
	} catch (Exception e) {
		 ResponseUtils.responseFailure(response);
	}
  }
  
  /**
   * 跳转到编辑界面
   * 
   * @return
   */
  @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
  public String toUpdate(@PathVariable Integer id, Model model) {
    try {
    	MiguVideoDaylimitBean mmdBean = miguVideoDaylimitService.findDaylimit(id);;
    	model.addAttribute("mmdBean", mmdBean);
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
    return "miguVideoDaylimit/update";
  }
  
  /**
   * 修改
   */
  @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
  @ResponseBody
  public void update(@PathVariable Integer id, HttpServletResponse response, MiguVideoDaylimitBean mmdBean) {
    try {
    	String name = mmdBean.getName();
		MiguVideoConfigBean miguVideoConfigBean = miguVideoDaylimitService.hasName(name);
		if (miguVideoConfigBean == null) {
			ResponseUtils.responseMarkNotExists(response);
		} else {
			mmdBean.setMark(miguVideoConfigBean.getChannelId() + "_" + miguVideoConfigBean.getVersionCode());
			mmdBean.setId(id);
	    	mmdBean.setIsExecute(0);
	    	miguVideoDaylimitService.modifyDaylimit(mmdBean);
	    	ResponseUtils.responseSuccess(response);
		}
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      ResponseUtils.responseFailure(response);
    }
  }
  
  /**
   * 删除
   */
  @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
  public void delete (@PathVariable Integer id,  HttpServletResponse response) {
	try {
		miguVideoDaylimitService.delete(id);
		ResponseUtils.responseSuccess(response);
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
	    ResponseUtils.responseFailure(response);
	}
  }


}
